package xxy.com.customview.view;

import android.content.Context;
import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.NestedScrollingChild;
import androidx.core.view.NestedScrollingChildHelper;
import androidx.core.view.ViewCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by Administrator on 2017/12/12.
 */

public class MyBehaviorChild extends CoordinatorLayout.Behavior implements NestedScrollingChild {

    private NestedScrollingChildHelper nestedScrollingChildHelper;   //帮助实现nestedScrollingChild中的方法的助手
    private int lastY;
    private int lastX;
    private int[] consumed = new int[2];
    private int[] offsetWindow = new int[2];

    public MyBehaviorChild(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(CoordinatorLayout parent, View child, MotionEvent ev) {
        if (nestedScrollingChildHelper == null) {
            nestedScrollingChildHelper = new NestedScrollingChildHelper(child);
            //设置可以嵌套滑动
            setNestedScrollingEnabled(true);
        }
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastY = (int) ev.getY();
                lastX = (int) ev.getX();
                //因为这里的事件是从coordinatorlayout分发下来的，所以整个页面包括这个View都是可以滑动到，所以要筛出这个view以外的地方，只让View以内的地方接受事件，并发出去
                if (lastY < child.getTop() || lastY > child.getBottom() || lastX < child.getLeft() || lastX > child.getRight()) {
                    return  true;   //view以外的地方直接返回true不做处理
                }
                //把开始滑动的事件传给实现了NestedScrollingParent的类
                startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
                break;
            case MotionEvent.ACTION_MOVE:
                int nowY = (int) ev.getY();
                int nowX = (int) ev.getX();
                int deltaY = nowY - lastY;
                int deltaX = nowX - lastX;
                if (Math.abs(deltaY) > Math.abs(deltaX)) {   //如果y方向上的移动距离大于x方向上的移动距离
                    dispatchNestedPreScroll(deltaX, -deltaY, consumed, offsetWindow);   //把消耗触摸事件件传给实现了NestedScrollingParent的类
                }
                break;
            case MotionEvent.ACTION_UP:
                stopNestedScroll();   //把停止滑动事件件传给实现了NestedScrollingParent的类
                break;
        }

        return true;
    }

    /**
     * 实现了NestedScrollingChild的类需要实现的方法
     * @param enabled
     */
    @Override
    public void setNestedScrollingEnabled(boolean enabled) {
        nestedScrollingChildHelper.setNestedScrollingEnabled(enabled);
    }

    @Override
    public boolean isNestedScrollingEnabled() {
        return nestedScrollingChildHelper.isNestedScrollingEnabled();
    }

    @Override
    public boolean startNestedScroll(int axes) {
        return nestedScrollingChildHelper.startNestedScroll(axes);
    }

    @Override
    public void stopNestedScroll() {
        nestedScrollingChildHelper.stopNestedScroll();
    }

    @Override
    public boolean hasNestedScrollingParent() {
        return nestedScrollingChildHelper.hasNestedScrollingParent();
    }

    @Override
    public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, @Nullable int[] offsetInWindow) {
        return nestedScrollingChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow);
    }

    @Override
    public boolean dispatchNestedPreScroll(int dx, int dy, @Nullable int[] consumed, @Nullable int[] offsetInWindow) {
        return nestedScrollingChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);
    }

    @Override
    public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
        return nestedScrollingChildHelper.dispatchNestedFling(velocityX, velocityY, consumed);
    }

    @Override
    public boolean dispatchNestedPreFling(float velocityX, float velocityY) {
        return nestedScrollingChildHelper.dispatchNestedPreFling(velocityX, velocityY);
    }
}
